package com.zxc.item.service.impl;

import com.zxc.common.enums.ShoppingEnum;
import com.zxc.common.exception.ShoppingException;
import com.zxc.item.entity.TbCategory;
import com.zxc.item.service.TbCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zxc.item.dao.TbCategoryMapper;

import java.util.List;

@Service
public class TbCategoryServiceImpl implements TbCategoryService {

    @Autowired
    private TbCategoryMapper tbCategoryMapper;

    @Override
    public List<TbCategory> getCategoryByParentId(Long parentId) {
        TbCategory tbCategory = new TbCategory();
        tbCategory.setParentId(parentId);

        List<TbCategory> tbCategoryList = tbCategoryMapper.select(tbCategory);

        //如果不存在数据，抛出自定义异常（在公共模块的枚举中，声明异常的类型）
        if (tbCategoryList.isEmpty()){
            throw new ShoppingException(ShoppingEnum.CATEGORY_LIST_NOT_FOUND);
        }
        return tbCategoryList;
    }

    @Override
    public List<TbCategory> getAllCategory() {
        List<TbCategory> tbCategoryList = tbCategoryMapper.selectAll();
        return tbCategoryList;
    }
}
